package wxdb.parse.absyn;
import wxdb.record.*;

import wxdb.symbol.Symbol;
public class CrtExp extends Exp{
	public Symbol tableName;
	public CTLst ctClauses;
	
	public CrtExp(int p, Symbol tn, CTLst cc){
		pos = p;
		tableName = tn;
		ctClauses = cc;
	}
	
	
	
	public String tableName(){
		return tableName.toString();
	}
	
	public Schema newSchema(){
		Schema sch = new Schema();
		CTLst lst = ctClauses;
		boolean isKey;
		int len;
		String fldName;
		int type;
		CTFld ctfld;
		FldsExp fldsexp;
		FldExp fldexp;
		while(lst != null){
			ctfld = lst.ctFld;
			if(ctfld instanceof NormalCTFld){
				isKey = false;
				fldName = ((NormalCTFld)ctfld).fieldName.toString();
				type = ((NormalCTFld)ctfld).type.optn;
				if(type == Type.VARCHAR){
					len = ((NormalCTFld)ctfld).constrain.ival;
					sch.addStringField(fldName, len,isKey);
				}else if(type == Type.INTEGER){
					sch.addIntField(fldName, isKey);
				}
			}else if(ctfld instanceof KeyTpCTFld){
				isKey = true;
				fldsexp = ((KeyTpCTFld)ctfld).fldsExp;
				while(fldsexp != null){
					fldexp = fldsexp.fldExp;
					if(fldexp instanceof NameFldExp){
						fldName = ((NameFldExp)fldexp).getName();
						sch.changeIsKey(fldName, isKey);
					}
					fldsexp = fldsexp.tail;
				}
			}
			lst = lst.tail;
		}
		return sch;
	}
}
